﻿Imports ATS.HelperClass
Imports ATS.CommonClass
Imports ATS.EntryValidation


Partial Public Class ucDepartments2

    Dim dbDepartments As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Private _pageIsValid As Boolean = True
    Dim editFlag As Integer
    Dim al As New ArrayList

    Sub pageIsValid(ByVal sender As String, ByVal isValid As Boolean)
        Dim inArray As Boolean = False
        For i = 0 To al.Count - 1
            If al.Item(i) = sender Then
                If isValid Then
                    al.Remove(Trim(sender))
                End If
                inArray = True
                Exit For
            End If
        Next
        If Not inArray Then
            If Not isValid Then
                al.Add(Trim(sender))
            End If
        End If
        If al.Count > 0 Then
            _pageIsValid = False
        Else
            _pageIsValid = True
        End If
    End Sub

    Private Sub ucDepartments_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        editFlag = HelperClass.EditMode.Load
        LoadDepartments()
        DisableAllControls()
    End Sub

    Private Sub DisableAllControls()
        HelperClass.DisableTextBox(gridDepartmentDetails)
        HelperClass.DisablePasswordBox(gridDepartmentDetails)
        HelperClass.DisableCheckBox(gridDepartmentDetails)
        HelperClass.DisableComboBox(gridDepartmentDetails)
    End Sub
    Private Sub EnableAllControls()
        HelperClass.EnableTextBox(gridDepartmentDetails)
        HelperClass.EnablePasswordBox(gridDepartmentDetails)
        HelperClass.EnableCheckBox(gridDepartmentDetails)
        HelperClass.EnableComboBox(gridDepartmentDetails)
    End Sub
    Private Sub ClearAllControls()
        HelperClass.ClearTextBox(gridDepartmentDetails)
        HelperClass.ClearPasswordBox(gridDepartmentDetails)
        HelperClass.ClearCheckBox(gridDepartmentDetails)
        HelperClass.ClearComboBox(gridDepartmentDetails)
    End Sub

    Private Sub LoadDepartments(Optional ByVal userFilter As String = "")
        Dim DepartmentTable = From DepartmentDetails In dbDepartments.Departments _
                         Where DepartmentDetails.DepartmentName.ToUpper.Contains(userFilter.ToUpper) _
                         Order By DepartmentDetails.DepartmentName

        listBoxDepartments.ItemsSource = DepartmentTable
        listBoxDepartments.SelectedIndex = 0
        LoadFields(DepartmentTable)
        If DepartmentTable.Count = 1 Then
            ExpanderText.Text = DepartmentTable.Count & " record found"
        Else
            ExpanderText.Text = DepartmentTable.Count & " records found"
        End If
    End Sub
    Private Sub LoadSelectedDepartment(Optional ByVal DepartmentFilter As String = "")
        Dim DepartmentTable = From DepartmentDetails In dbDepartments.Departments _
                         Where DepartmentDetails.DepartmentName.ToUpper.Contains(DepartmentFilter.ToUpper) _
                        Order By DepartmentDetails.DepartmentName

        LoadFields(DepartmentTable)
    End Sub

    Sub LoadFields(ByVal departmentDetails As IQueryable)
        Dim lf = From depDetails In departmentDetails
        If lf.Count > 0 Then
            With lf.First
                textboxCode.Text = .DepartmentCode
                textboxName.Text = .DepartmentName
                textboxDescription.Text = .DepartmentDesc
            End With
            ucActions.buttonEditMain.IsEnabled = True
            ucActions.buttonDeleteMain.IsEnabled = True
        Else
            ClearAllControls()
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            'textboxCode.Text = Nothing
        End If
        pageIsValid(textboxCode.Name, EntryValidation.ValidateText(textboxCode))
        pageIsValid(textboxName.Name, EntryValidation.ValidateText(textboxName))
    End Sub


    Private Sub listBoxDepartments_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listBoxDepartments.SelectionChanged
        Dim selectedDepartment As String = ""
        If Not listBoxDepartments.SelectedItem Is Nothing Then
            selectedDepartment = listBoxDepartments.SelectedItem.DepartmentName.ToString
        End If
        LoadSelectedDepartment(selectedDepartment)
    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchButton.Click
        LoadDepartments(searchBox.Text)
    End Sub

    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
        LoadDepartments(searchBox.Text)
    End Sub

    Private Sub ucActions_AddButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.AddButtonClicked
        gridListbox.IsEnabled = False
        editFlag = EditMode.Add
        ClearAllControls()
        EnableAllControls()
    End Sub

    Private Sub ucActions_CancelButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.CancelButtonClicked
        Dim selectedDepartment As String = ""
        If Not listBoxDepartments.SelectedValue Is Nothing Then
            selectedDepartment = listBoxDepartments.SelectedItem.DepartmentName.ToString
        End If


        editFlag = EditMode.Load
        DisableAllControls()
        LoadSelectedDepartment(selectedDepartment)
        gridListbox.IsEnabled = True
    End Sub

    Private Sub ucActions_DeleteButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.DeleteButtonClicked
        Try
            Dim warningString As String = "Do you really want to delete this record?" & ControlChars.Lf & ControlChars.Lf & listBoxDepartments.SelectedItem.Departmentname.ToString
            If System.Windows.MessageBox.Show(warningString, "Delete Record", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                Dim selectedDepartmentCode As String = listBoxDepartments.SelectedItem.Departmentcode.ToString
                Dim DepartmentsTable = From DepartmentDetails In dbDepartments.Departments _
                             Where DepartmentDetails.DepartmentCode.ToUpper = (selectedDepartmentCode.ToUpper)
                Dim exst As Department = DepartmentsTable.First
                dbDepartments.Departments.DeleteOnSubmit(exst)
                dbDepartments.SubmitChanges()
                LoadDepartments()
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
        End Try
    End Sub

    Private Sub ucActions_EditButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.EditButtonClicked
        editFlag = EditMode.Edit
        gridListbox.IsEnabled = False
        EnableAllControls()
        textboxCode.IsEnabled = False
    End Sub

    Private Sub ucActions_SaveButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.SaveButtonClicked
        Try
            If _pageIsValid Then
                Dim ListBoxSelectedItemIndex As Integer = listBoxDepartments.SelectedIndex
                If editFlag = EditMode.Edit Then
                    Dim DepartmentsTable = From DepartmentDetails In dbDepartments.Departments _
                                 Where DepartmentDetails.DepartmentCode = textboxCode.Text.ToUpper

                    With DepartmentsTable.First
                        .DepartmentDesc = textboxDescription.Text
                        .DepartmentName = textboxName.Text
                    End With
                    dbDepartments.SubmitChanges()
                ElseIf editFlag = EditMode.Add Then
                    Dim saveNewDepartment As New Department
                    With saveNewDepartment
                        .DepartmentDesc = textboxDescription.Text
                        .DepartmentName = textboxName.Text
                        .DepartmentCode = textboxCode.Text
                    End With
                    dbDepartments.Departments.InsertOnSubmit(saveNewDepartment)
                    dbDepartments.SubmitChanges()
                End If
                editFlag = EditMode.Load
                If searchBox.Text.Length > 0 Then
                    LoadDepartments(searchBox.Text)
                Else
                    LoadDepartments()
                End If

                listBoxDepartments.SelectedIndex = ListBoxSelectedItemIndex
                DisableAllControls()
                gridListbox.IsEnabled = True
                _pageIsValid = True
            Else
                ucActions.buttonSaveMain.IsEnabled = True
                ucActions.buttonEditMain.IsEnabled = False
                ucActions.buttonAddMain.IsEnabled = False
                ucActions.buttonDeleteMain.IsEnabled = False
                ucActions.buttonCancelMain.IsEnabled = True
                Dim warningString As String = "Some mandatory fields are missing."
                System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
            ucActions.buttonSaveMain.IsEnabled = True
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonAddMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            ucActions.buttonCancelMain.IsEnabled = True
        End Try
    End Sub

    Private Sub textboxCode_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxCode.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

        If editFlag = EditMode.Add Then
            Dim selectedDepartment As String = textboxCode.Text.ToString
            Dim DepartmentTable = From DepartmentDetails In dbDepartments.Departments _
                         Where DepartmentDetails.DepartmentCode = selectedDepartment.ToUpper
            If DepartmentTable.Count > 0 And textboxCode.IsEnabled = True Then
                _pageIsValid = False
                textboxCode.BorderBrush = New SolidColorBrush(Colors.Red)
                textboxCode.ToolTip = "Code already exist."
            End If
        End If
    End Sub

    Private Sub textboxName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxName.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

    End Sub
End Class
